﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using NXDO.Data.Factory;
using NXDO.WebAPI.Controllers;
using System;
using System.Collections.Generic;
using System.Text;
using TDM.DbData.DataEntity;

namespace TDM.WebSvr.Controllers
{
    /// <summary>
    /// 激光试验结果接口
    /// </summary>
    [AllowAnonymous]
    public class TestLaserResultController : ApiDbEntityController<TestLaserResult, long>
    {
        protected override string GetPkName()
        {
            return "LaserDID";
        }

        protected override string GetTableName()
        {
            return "TestLaserResult";
        }

        /// <inheritdoc/>
        public override TestLaserResult Post([FromBody] TestLaserResult data)
        {
            //获取用户信息
            var user = HttpContext.User as NXDO.Data.Security.Principal.MemberPrincipal;
            long uid = Convert.ToInt64(user?.MemberId);
            if (uid == 0) throw new Exception("未获得登录信息");
            data.CreateUID = uid;
            data.CreateTime = DateTime.Now;
            return base.Post(data);
        }

        /// <inheritdoc/>
        public override bool Delete(long pkValue)
        {
            var sql = $"update {GetTableName()} set IsDelete = 1 where {GetPkName()} = {pkValue}";
            return DbHelper.SetUpdateCommand(sql).ExecuteNonQuery() > 0;
        }


        /// <summary>
        /// 根据staskid获取数据(不含已删除)
        /// </summary>
        /// <param name="staskID"></param>
        /// <returns></returns>
        [HttpGet]
        public TestLaserResult GetBySTaskID([FromQuery] long staskID)
        {
            var sql = $"select * from {GetTableName()} where IsDelete = 0 and staskID = {staskID} order by createtime desc";
            var rs = DbHelper.SetCommand(sql).ExecuteEntityScalar<TestLaserResult>();
            if (rs == null) rs = new TestLaserResult();
            return rs;
        }
    }
}
